import { Component, ElementRef, EventEmitter, forwardRef, Input, OnChanges, Output, SimpleChanges, ViewChild } from "@angular/core";
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";

@Component({
  selector: 'app-design-form-process-node',
  templateUrl: './design-form-process-node.component.html',
  styleUrls: ['./design-form-process-node.component.scss'],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => DesignFormProcessNodeComponent),
      multi: true
    }
  ]

})
export class DesignFormProcessNodeComponent implements ControlValueAccessor, OnChanges {
  progressNodes
  onChange: (_: any) => void = (_: any) => { };
  onTouched: () => void = () => { };
  writeValue(obj: any): void {
    this.progressNodes = obj;
    if (this.progressNodes) {
      this.initProcess()
    }
  }
  registerOnChange(fn: any): void {
    this.onChange = fn
  }
  registerOnTouched(fn: any): void {
    this.onTouched = fn;
  }
  setDisabledState?(isDisabled: boolean): void {
  }
  ngOnChanges(changes: SimpleChanges): void {
    this
  }

  showProcessDetail(node) {
    /*
    let dialogRef = this.dialog.open(ShowProcessDetailDialog, {
        width: '',
        disableClose: true,
        data: {
            info: node
        }
    });
    dialogRef.afterClosed().subscribe(res => {
    });
    */
  }
  
  showProcessIcon(i) {
    let len = this.progressNodes.length
    return (((((i + 1) % 5) == 0 && Math.ceil((i + 1) / 5) % 2 != 0) || (i == len - 1 && Math.ceil((i + 1) / 5) % 2 != 0)) || ((((i + 1) % 5) == 0 && Math.ceil((i + 1) / 5) % 2 == 0) || (i == len - 1 && Math.ceil((i + 1) / 5) % 2 == 0)))
      && (i < this.progressNodes.length - 1)
  }

  initProcess() {
    var i, j, len, len1, num, ref, results, rows;
    this.progressNodes.sort(function (a, b) {
        return parseInt(a.operate_date.toString().replace(/-/g, ''), 10) - parseInt(b.operate_date.toString().replace(/-/g, ''), 10);
    });
    ref = this.progressNodes;
    results = [];
    for (num = j = 0, len1 = ref.length; j < len1; num = ++j) {
        let strI = ((i + 1) / 5).toString()
        rows = ref[num];
        i = num;
        len = this.progressNodes.length;
        if (i == 0) {
            rows["class"] = '_first'
            continue
        }
        if ((i + 1) % 5 === 1 && parseInt(strI) % 2 !== 0) {
            if (i === len - 1) {
                rows["class"] = 'whole_last_right';
                continue;
            }
            results.push(rows["class"] = '_last');
        } else if ((i + 1) % 5 === 1 && parseInt(strI) % 2 === 0) {
            if (i === len - 1) {
                rows["class"] = 'whole_last_left';
                continue;
            }
            results.push(rows["class"] = '_first');
        } else if ((((i + 1) % 5) === 0 && Math.ceil((i + 1) / 5) % 2 !== 0) || (i === len - 1 && Math.ceil((i + 1) / 5) % 2 !== 0)) {
            results.push(rows["class"] = 'last');
        } else if ((((i + 1) % 5) === 0 && Math.ceil((i + 1) / 5) % 2 === 0) || (i === len - 1 && Math.ceil((i + 1) / 5) % 2 === 0)) {
            results.push(rows["class"] = 'first');
        } else if (parseInt(strI) % 2 === 0) {
            results.push(rows["class"] = 'middle');
        } else {
            results.push(rows["class"] = '_middle');
        }
    }
    return results;
}
}